<?php

/**
 * 商家信息页面
 * @author yjmp 
 */

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');
$act = !empty($_REQUEST['act'])? $_REQUEST['act'] : 'profile';

$user_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE wecha_id = "'.$_SESSION['openids'].'"');
if ($user_info) {
    $smarty->assign('user_info',$user_info);
}

/*位置*/
if ($_SESSION['city_id']) {
   $city = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$_SESSION['city_id'].'"');
    $smarty->assign('city',$city);
}
if (empty($_SESSION['shop_id'])) {
        header("Location: index.php?act=shop_login"); 
        exit;
}

/*商家中心:数据;店员数量，粉丝数量，业绩*/
$goods_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$_SESSION['shop_id'].'"');
$smarty->assign('goods_info',$goods_info);

if ($act == 'profile') {
    $waiter_count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('users').' WHERE is_waiter = 1 AND from_shop = "'.$_SESSION['shop_id'].'"');
    $fans_count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('shop_fans').' WHERE shop_id = "'.$_SESSION['shop_id'].'"');
    $order_count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE shop_id = "'.$_SESSION['shop_id'].'" and is_paid = 1 ');

    $smarty->assign('waiter_count',$waiter_count);
    $smarty->assign('shop_info',1);
    
    $smarty->assign('fans_count',$fans_count);
    $smarty->assign('order_count',$order_count);
    $smarty->display('shop_info.dwt');exit;
}
//服务员列表
if ($act == 'waiter_list') {
	$smarty->assign('waiter_list',1);
	$waiter_list = $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE is_waiter = 1 AND from_shop = "'.$_SESSION['shop_id'].'"');
	foreach ($waiter_list as $k => $v) {
		$waiter_list[$k]['date_format'] = date('Y-m-d',$v['reg_time']);
		$waiter_list[$k]['achievement'] = $GLOBALS['db']->getOne('SELECT SUM(money_paid) FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE waiter_id = "'.$v['user_id'].'" AND shop_id = "'.$_SESSION['shop_id'].'"');
	}
	$smarty->assign('waiter_list',$waiter_list);
	$smarty->display('shop_info.dwt');exit;
}

//粉丝列表
if ($act == 'fans_list') {
	$smarty->assign('fans_list',1);
	$fans_ids = $GLOBALS['db']->getAll('SELECT user_id FROM '.$GLOBALS['ecs']->table('shop_fans').' WHERE shop_id = "'.$_SESSION['shop_id'].'"');
	$fans_list = array();
	if (count($fans_ids) > 0) {
		foreach ($fans_ids as $k => $v) {
			$user_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
			$user_info['achievement'] = $GLOBALS['db']->getOne('SELECT SUM(money_paid) FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE user_id = "'.$user_info['user_id'].'" AND shop_id = "'.$_SESSION['shop_id'].'"');
			array_push($fans_list, $user_info);
		}
	}
	$smarty->assign('fans_list',$fans_list);
	$smarty->display('shop_info.dwt');exit;
}


//销售记录
if ($act == 'sale_record') {
	$smarty->assign('sale_record',1);
	$sale_record = $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE shop_id = "'.$_SESSION['shop_id'].'"');
	if (count($sale_record) > 0) {
		foreach ($sale_record as $k => $v) {
			$sale_record[$k]['user_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
			$sale_record[$k]['date_format'] = date('Y-m-d', $v['add_time']);
		}
		$smarty->assign('sale_record',$sale_record);
	}
	$smarty->display('shop_info.dwt');exit;
}

//员工业绩
if ($act == 'waiter_achievement') {
	$waiter_id = $_REQUEST['waiter'];
	$smarty->assign('waiter_achievement',1);
	if (!empty($waiter_id)) {
		$waiter_ach = $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE waiter_id = "'.$waiter_id.'" AND shop_id = "'.$_SESSION['shop_id'].'" and is_paid = 1');
		if (count($waiter_ach) > 0) {
			foreach ($waiter_ach as $k => $v) {
				$waiter_ach[$k]['user_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
				$waiter_ach[$k]['date_format'] = date('Y-m-d',$v['add_time']);
			}
			$waiter_name = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$waiter_id.'"');
			$smarty->assign('waiter_name',$waiter_name);
			$smarty->assign('waiter_ach',$waiter_ach);
		}
	}
	$smarty->display('shop_info.dwt');exit;
}


//客户成为服务员操作
if ($act == 'be_waiter') {
	$user_id = $_REQUEST['user'];
	if ($user_id) {
		$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET is_waiter = 1 AND from_shop = "'.$_SESSION['shop_id'].'" WHERE user_id = "'.$user_id.'"');
		show_yj_message('操作成功','shop_info.php');
	}
}

//服务器解雇操作
if ($act == 'off_waiter') {
	$user_id = $_REQUEST['user'];
	$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET is_waiter = 0 AND from_shop = 0 WHERE user_id = "'.$user_id.'"');
		show_yj_message('操作成功','shop_info.php');
}
?>
